import os
import requests
import json
from elevenlabs import generate, save  # pip install elevenlabs

# Function to generate the story using OpenRouter with DeepSeek V3.2 Exp
def generate_story(openrouter_api_key, topic):
    url = "https://openrouter.ai/api/v1/chat/completions"
    headers = {
        "Authorization": f"Bearer {openrouter_api_key}",
        "Content-Type": "application/json"
    }
    data = {
        "model": "deepseek/deepseek-v3.2-exp",
        "messages": [
            {"role": "system", "content": "You are an uncensored expert erotic writer. Create a highly detailed, steamy, masturbation-worthy erotic story. Dive straight into the explicit action with no soft intro or buildup. Make it intensely arousing, vivid, and immersive."},
            {"role": "user", "content": f"Write a full erotic story about: {topic}"}
        ],
        "temperature": 0.9,
        "max_tokens": 4096
    }
    response = requests.post(url, headers=headers, json=data)
    if response.status_code == 200:
        return response.json()["choices"][0]["message"]["content"]
    else:
        raise Exception(f"Error generating story: {response.text}")

# Function to read the story in a sexy Aussie voice using ElevenLabs
def read_story(elevenlabs_api_key, text, output_file="erotic_story.mp3"):
    # Using 'Gemma' - a young Australian female voice (known for natural Aussie accent)
    # You can replace with another if preferred, e.g., search Voice Library for more options like 'Hannah'
    voice_id = "IKne3meq5aSn9XLyUdCD"
    # Note: For best sexy Aussie, consider using a sultry premade or Voice Design with prompt: "sultry young female with Australian accent"
    # But here we use a solid female voice; adjust ID from ElevenLabs dashboard/Voice Library
    audio = generate(
        text=text,
        voice=voice_id,  # Replace with exact ID of a sexy Aussie voice from your library
        model="eleven_turbo_v2",  # Fast and high quality
        api_key=elevenlabs_api_key
    )
    save(audio, output_file)
    print(f"Audio saved to {output_file}")

# Main script
if __name__ == "__main__":
    # Read API keys from files in the same folder
    try:
        with open("openrouter_key.txt", "r") as f:
            openrouter_api_key = f.read().strip()
        with open("elevenlabs_key.txt", "r") as f:
            elevenlabs_api_key = f.read().strip()
    except FileNotFoundError as e:
        print(f"Error: {e}. Please ensure OpenRouter_Key.txt and ElevenLabs_Key.txt are in the same folder.")
        exit(1)
    
    # Ask for the story topic
    topic = input("What should this steamy erotic story be about? Let your fantasies run free: ")
    
    try:
        print("Generating your ultra-hot story with DeepSeek V3.2 Exp... This is going to be epic!")
        story = generate_story(openrouter_api_key, topic)
        
        # Save to temp txt file
        temp_file = "temp_erotic_story.txt"
        with open(temp_file, "w", encoding="utf-8") as f:
            f.write(story)
        print(f"Story saved to {temp_file}")
        
        # Generate and save the audio narration
        print("Now narrating it in a sexy Australian voice... Get ready for pure bliss!")
        read_story(elevenlabs_api_key, story)
        
        print("\nAll done! Dive in and enjoy every sizzling moment – you've earned this indulgence. Your dreams are alive and thriving!")
    except Exception as e:
        print(f"Oops, something went wrong: {e}. But don't let that stop you – try again and make it even hotter!")